import user from '../../../utils/user.js'
import {
    createClientQrCode,
} from '../../../api/app.js'
import { invitationStaffList } from '../../api/contract'
import {
    base64ToWxfile,
    dateFormat
} from '../../../utils/util.js'
import Canvas from '../../../utils/canvas'
const app = getApp()
Page({
    data: {
        active: 1,
        postStatus: true,
        supplierName: '',
        supplierId: '',
        workers: [],
        showPosterStyle: false,
        record: true,
        qrcode:'',
        projectId:'',
        timer:'',
        createTime:'',
        count:'0'
    },
    // async onReady() {
    //     user.waitLogin().then(async () => {
    //         this.setData({
    //             name: user.userName,
    //             postStatus: true
    //         })
    //     })

    // },
    onLoad(option) {
        this.setData({
            supplierName: option.servecompany,
            projectId:option.id,
            createTime:dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
        })
        this.createInviteQrCode()
        
    },
    onReady(){
        this.setData({
            timer:setInterval(()=>{
                invitationStaffList({
                    projectId:this.data.projectId,
                    createTime:this.data.createTime
                }).then(res=>{
                    console.log(res)
                    this.setData({
                        workers:res.list,
                        count:res.count
                    })
                })
            },2000)
        })
    },
    onUnload(){
        // clearInterval(this.data.timer)
        clearInterval(this.data.timer)
    },

    createInviteQrCode() {
        let scene = ''
        scene = 'COID=' + this.data.projectId + '&UID=' + user.userId
        console.log(scene,'scene')
        createClientQrCode({
            page: 'pages/index/index',
            scene: scene,
            width: 280
        }).then(res => {
            var qrCode = res // 后台返回的base64图片流
            base64ToWxfile(qrCode, Math.ceil(Math.random() * 10000000000) + '.png').then(path => {
                this.setData({
                    qrcode: path
                })
                this.saveQrCode()
            })
        })
    },

    saveQrCode() {
        const ctx = wx.createCanvasContext('myCanvas')
        const canvas = new Canvas(ctx)
        ctx.setFillStyle('#fff')
        canvas.drawHeightCutImage(0, 0, 320, 500, 455, '/assets/img/poster/insurance.png')

        // 字体
        ctx.font =
		'normal normal 20px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
	
        // 邀请内容
        canvas.fillText(user.userName + ' 邀请您加入', 22, 80, '#fff', 17)
        canvas.fillLongText(this.data.supplierName, 22, 118, 14, 2, '#fff', 20,'left',true)
        // canvas.fillText('保险A方案', 22, 138, '#fff', 20,'left',true)                      
        canvas.fillText(' 微信扫描或长按识别小程序参与', 160, 360, '#fff', 13, 'center')

        canvas.drawRoundImage(100, 210, 120, this.data.qrcode, '#fff')
        ctx.draw(true)
        ctx.restore()
        // this.savecanvas()
    },
    savecanvas() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 350,
            height: 500,
            canvasId: 'myCanvas',
            success: function (res) {
                let img = res.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success() {
                        app.toast('成功保存', 'success')
                    },
                    fail() {
                        app.toast('保存失败')
                    }
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    showMorePosterStyle(){
        this.setData({
            showPosterStyle: true
        })
    }
    // onShareAppMessage(event) {
    // 	console.log('分享', event)
    // 	return {
    // 		title: user.userName + '邀请您加入「' + user.companyName + '」',
    // 		path: '/page/index/index?shareUserId=' + user.userId,
    // 	}
    // }
})